package io.virtualapp.home;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.virtualapp.R;
import io.virtualapp.VCommends;
import io.virtualapp.abs.ui.VFragment;
import io.virtualapp.bean.Gds;
import io.virtualapp.bean.NumBean;
import io.virtualapp.bean.Swt;
import io.virtualapp.bean.UpdateBean;
import io.virtualapp.bean.Vip;
import io.virtualapp.mylibrary.callback.BaseCallback;
import io.virtualapp.mylibrary.http.HttpUtils;
import io.virtualapp.mylibrary.util.SpUtils;
import okhttp3.Request;
import okhttp3.Response;
import zxing.MainActivity;
import zxing.activity.BusinessCardActivity;
import zxing.activity.LoveLetterActivity;
import zxing.activity.QRHttpActivity;
import zxing.activity.Record1Activity;
import zxing.activity.RecordActivity;
import zxing.activity.TextCodeActivity;
import zxing.activity.VipPayActivity;
import zxing.activity.WifiCodeActivity;
import zxing.activity.utils.AppConstans;
import zxing.android.CaptureActivity;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by cheng
 * PackageName VirtualApp
 * 2018/8/3 10:41
 */

public class MakeFragment extends VFragment implements View.OnClickListener {
    LinearLayout text_code, lin_card, lin_wifi, lin_http, lin_love, lin_record;
    private HomeContract.HomePresenter mPresenter;
    private boolean isFirstInApp;
    private long surplus;//剩余时间
    Vip vip1;
    private List<NumBean> datas;
    int i = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_make, null);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            View decorView = getActivity().getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        text_code = view.findViewById(R.id.text_code);
        lin_card = view.findViewById(R.id.lin_card);
        lin_wifi = view.findViewById(R.id.lin_wifi);
        lin_http = view.findViewById(R.id.lin_http);
        lin_love = view.findViewById(R.id.lin_love);
        lin_record = view.findViewById(R.id.lin_record);
        lin_record.setOnClickListener(this);
        lin_love.setOnClickListener(this);
        lin_wifi.setOnClickListener(this);
        lin_http.setOnClickListener(this);
        lin_card.setOnClickListener(this);
        text_code.setOnClickListener(this);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("share", MODE_PRIVATE);
        boolean isFirstRun = sharedPreferences.getBoolean("isFirstRun", true);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        if (isFirstRun) {
            editor.putBoolean("isFirstRun", false);
            long firstTime = System.currentTimeMillis();
            SpUtils.getInstance().putLong(VCommends.APPFIRSTINTIME, firstTime);
            editor.commit();
        }
        getNun();
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
        updataVip();
        getNun();
    }
    public void getNun() {
        datas = new ArrayList<>();
        datas = DataSupport.findAll(NumBean.class);
        if (datas != null && !datas.isEmpty()) {
            i = datas.get(0).getNum();
        }
    }
    long everyTime;
    long oldTime;

    protected void updataVip() {
        HttpUtils.getInstance().postUpdate(new BaseCallback<UpdateBean>() {
            @Override
            public void onRequestBefore() {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onSuccess(Response response, UpdateBean o) {
                if (o != null && response.isSuccessful()) {
                    vip1 = o.getVip();
                    if (DataSupport.findFirst(Vip.class) != null) {
                        DataSupport.deleteAll(Vip.class);
                    }
                    vip1.save();

                }
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {

            }
        });
    }

    public void getVIP() {
        List<Swt> swts = DataSupport.findAll(Swt.class);
        if (swts.size() > 0) {
            for (int i = 0; i < swts.size(); i++) {
                if (swts.get(i).getCode().equals("S1260001")) {
                    surplus = swts.get(i).getVal1() * 60 * 1000;
                }
            }
        }
        everyTime = System.currentTimeMillis(); //每次进入时间
        oldTime = SpUtils.getInstance().getLong(VCommends.APPFIRSTINTIME);

    }

    private void showistimeexpirseAlerDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("温馨提示");
        builder.setMessage("您的试用时间已过，请开通会员！");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                startActivity(new Intent(getActivity(), VipPayActivity.class));
            }
        });
        builder.show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.text_code:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) { //如果进入时间超过24小时，app可用时间过期
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), TextCodeActivity.class));
                break;
            case R.id.lin_card:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) {
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), BusinessCardActivity.class));
                break;
            case R.id.lin_wifi:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) {
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), WifiCodeActivity.class));
                break;
            case R.id.lin_http:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) {
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), QRHttpActivity.class));
                break;
            case R.id.lin_love:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) {
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), LoveLetterActivity.class));
                break;
            case R.id.lin_record:
//                getVIP();
                if (DataSupport.findFirst(Vip.class) != null) {
                    vip1 = DataSupport.findFirst(Vip.class);
                }
                if (vip1.isIsout() && i > 0) {
                    showistimeexpirseAlerDialog();
                    return;
                } else
                startActivity(new Intent(getContext(), Record1Activity.class));
                break;
            default:
                break;
        }
    }
}





